package com.javaee.xiangyang.vo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;

/**
 * 封装前端返回统一实体类
 */
//@JsonInclude(value= JsonInclude.Include.NON_NULL)
@JsonInclude()
public class ServerResponse<T> {

    private static final String DEFAULT_SUCCESS_MESSAGE = "SUCCESS";
    private static final String DEFAULT_FAIL_MESSAGE = "FAIL";
    private static final int RESULT_CODE_SUCCESS = 200;
    private static final int RESULT_CODE_SERVER_ERROR = 500;

    private int code;//状态
    private T data;//封装内容
    private String msg;//提示信息

    private ServerResponse() {
    }

    private ServerResponse(int code) {
        this.code = code;
    }

    private ServerResponse(int code, T data) {
        this.code = code;
        this.data = data;
    }

    private ServerResponse(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private ServerResponse(int code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    //@JsonIgnore
    //判断接口连接是否成功
    public boolean isSuccess(){
        return this.code == 200;
    }

    //判断是否存在
    public static <T> ServerResponse<T> creatServerResponseBySuccess(){
        return new ServerResponse<T>(RESULT_CODE_SUCCESS);
    }
    //成功调用接口
    public static <T> ServerResponse<T> creatServerResponseBySuccess(T data){
        return new ServerResponse<T>(RESULT_CODE_SUCCESS, data, DEFAULT_SUCCESS_MESSAGE);
    }
    //内容不存在
    public static <T> ServerResponse<T> creatServerResponseByFail(String msg){
        return new ServerResponse<T>(RESULT_CODE_SERVER_ERROR, msg);
    }
    //连接接口失败
    public static <T> ServerResponse<T> creatServerResponseByFail(){
        return new ServerResponse<T>(RESULT_CODE_SERVER_ERROR, DEFAULT_FAIL_MESSAGE);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}
